package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseThreadLocal;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
    * 新增购物车
    * */
    @PostMapping("/add")
    public R<String> save(@RequestBody ShoppingCart shoppingCart){
        //获取用户id
        Long userId = BaseThreadLocal.get();
         /* 菜品或者套餐是否点过
            如何知道烧鹅被点过了？？ 根据烧鹅的菜品id，查询下数据库中有没有烧鹅这个数据
            如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
            如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
     */
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        //判断用户id是否相等
        qw.eq(ShoppingCart::getUserId,userId);
        //先判断点的是套餐还是单点菜品
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){//说明点的是菜品
            qw.eq(ShoppingCart::getDishId,dishId);
        }else {
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        if (one == null){
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
        }else {
            shoppingCart.setNumber(one.getNumber()+1);
            shoppingCartService.save(shoppingCart);
        }
        return R.success("添加成功");
    }
    /**
    * 查询购物车
    * */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //获取用户名
        Long userId = BaseThreadLocal.get();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        qw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }
    /**
    * 取消购物车
    * */
    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart){
        //获取用户名
        Long userId = BaseThreadLocal.get();
        //先判断点的是套餐还是单点菜品
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        if (dishId == null){
            qw.eq(ShoppingCart::getDishId,dishId);
        }else {
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        //获取购物车里菜品或者套餐数量
        Integer number = one.getNumber();
        //取消产品
        Integer number1 = number --;
        one.setNumber(number1);
        if (number1>0){//购物车还有
            shoppingCartService.updateById(one);
        }else {//<=0购物车已经没有菜品
            shoppingCartService.remove(qw);
        }
        return R.success(one);
    }

    /**
    * 清空购物车
    * */
    @DeleteMapping("/clean")
    public R<String> deleteShoppingCart(){
        LambdaQueryWrapper<ShoppingCart> qw =new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseThreadLocal.get());
        shoppingCartService.remove(qw);
        return R.success("清空购物车成功");
    }
}
